﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceAgent.PortfolioService;
using System.ServiceModel;
using System.Collections.ObjectModel;
using ServiceAgent.SystemService;

namespace ServiceAgent
{
    public class SystemServiceAgent : IDisposable
    {
        SystemServiceClient proxy;

        public SystemServiceAgent()
        {
            proxy = new SystemServiceClient();
        }

        public ObservableCollection<DonViDTO> GetDonViList()
        {
            try
            {
                proxy.Open();
                ObservableCollection<DonViDTO> list =
                    proxy.GetDonViList();
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        #region User Methods
        public ObservableCollection<UserDTO> GetUserList()
        {
            try
            {
                proxy.Open();
                ObservableCollection<UserDTO> list =
                    proxy.GetUserList();
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<UserDTO> GetUserByDonViID(int DonViIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<UserDTO> list =
                    proxy.GetUserByDonViID(DonViIDFilter);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public UserDTO GetUserByID(int IDFilter)
        {
            try
            {
                proxy.Open();
                UserDTO obj =
                    proxy.GetUserByID(IDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public UserDTO GetUserByTenTaiKhoan(string TenTaiKhoanFilter)
        {
            try
            {
                proxy.Open();
                UserDTO obj =
                    proxy.GetUserByTenTaiKhoan(TenTaiKhoanFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveUser(UserDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.SaveUser(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveUserWithDonViList(UserDTO objdto, ObservableCollection<DonViDTO> listdto)
        {
            try
            {
                proxy.Open();
                proxy.SaveUserWithDonViList(objdto, listdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveDonViWithQuyenList(DonViDTO objdto, ObservableCollection<QuyenDTO> listdto)
        {
            try
            {
                proxy.Open();
                proxy.SaveDonViWithQuyenList(objdto, listdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<DonViDTO> GetDonViNotYetByUserID(int UserID)
        {
            try
            {
                proxy.Open();
                ObservableCollection<DonViDTO> list =
                    proxy.GetDonViNotYetByUserID(UserID);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<DonViDTO> GetDonViByUserID(int UserID)
        {
            try
            {
                proxy.Open();
                ObservableCollection<DonViDTO> list =
                    proxy.GetDonViByUserID(UserID);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SetDonViListForUser(int UserID, ObservableCollection<DonViDTO> listDTO) {
            try
            {
                proxy.Open();
                proxy.SetDonViListForUser(UserID, listDTO);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void UnSetDonViListForUser(int UserID, ObservableCollection<DonViDTO> listDTO)
        {
            try
            {
                proxy.Open();
                proxy.UnSetDonViListForUser(UserID, listDTO);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemoveUser(UserDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.RemoveUser(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemoveUserByID(int IDFilter)
        {
            try
            {
                proxy.Open();
                proxy.RemoveUserByID(IDFilter);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void AssignUserToDonVi(int UserID, int DonViID)
        {
            try
            {
                proxy.Open();
                proxy.AssignUserToDonVi(UserID,DonViID);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void AssignUserListToDonVi(ObservableCollection<UserDTO> dtoList, int DonViID)
        {
            try
            {
                proxy.Open();
                proxy.AssignUserListToDonVi(dtoList, DonViID);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }
        #endregion

        #region Quyen Methods
        public ObservableCollection<QuyenDTO> GetQuyenList()
        {
            try
            {
                proxy.Open();
                ObservableCollection<QuyenDTO> list =
                    proxy.GetQuyenList();
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<QuyenDTO> GetQuyenByDonViID(int DonViIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<QuyenDTO> list =
                    proxy.GetQuyenByDonViID(DonViIDFilter);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<QuyenDTO> GetQuyenNotYetByDonViID(int DonViIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<QuyenDTO> list =
                    proxy.GetQuyenNotYetByDonViID(DonViIDFilter);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SetQuyenForDonVi(int DonViID, int QuyenID)
        {
            try
            {
                proxy.Open();
                proxy.SetQuyenForDonVi(DonViID,QuyenID);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SetQuyenListForDonVi(int DonViID, ObservableCollection<QuyenDTO> listDTO)
        {
            try
            {
                proxy.Open();
                proxy.SetQuyenListForDonVi(DonViID, listDTO);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void UnSetQuyenListForDonVi(int DonViID, ObservableCollection<QuyenDTO> listDTO)
        {
            try
            {
                proxy.Open();
                proxy.UnSetQuyenListForDonVi(DonViID, listDTO);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }
        #endregion

        #region Disposable
        public void Dispose()
        {
            this.DisposeExistingServiceClientIfRequired();
        }

        private void DisposeExistingServiceClientIfRequired()
        {
            if (proxy != null)
            {
                try
                {
                    if (proxy.State == CommunicationState.Faulted)
                    {
                        proxy.Abort();
                    }
                    else
                    {
                        proxy.Close();
                    }
                }
                catch
                {
                    proxy.Abort();
                }

                proxy = null;
            }
        }
        #endregion
    }
}
